package com.ruoyi.haircut.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.haircut.domain.OrderStatistics;
import org.apache.poi.hpsf.Decimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.haircut.mapper.OrderMapper;
import com.ruoyi.haircut.domain.Order;
import com.ruoyi.haircut.service.IOrderService;

/**
 * 结账Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-13
 */
@Service
public class OrderServiceImpl implements IOrderService
{
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询结账
     *
     * @param id 结账主键
     * @return 结账
     */
    @Override
    public Order selectOrderById(Long id)
    {
        return orderMapper.selectOrderById(id);
    }

    /**
     * 查询结账列表
     *
     * @param order 结账
     * @return 结账
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        return orderMapper.selectOrderList(order);
    }

    /**
     * 新增结账
     *
     * @param order 结账
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        order.setCreateTime(DateUtils.getNowDate());
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改结账
     *
     * @param order 结账
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除结账
     *
     * @param ids 需要删除的结账主键
     * @return 结果
     */
    @Override
    public int deleteOrderByIds(Long[] ids)
    {
        return orderMapper.deleteOrderByIds(ids);
    }

    /**
     * 删除结账信息
     *
     * @param id 结账主键
     * @return 结果
     */
    @Override
    public int deleteOrderById(Long id)
    {
        return orderMapper.deleteOrderById(id);
    }

    @Override
    public Map<String, Object> getOrderStatistics(OrderStatistics orderStatistics) {
        HashMap<String, Object> result = new HashMap<>();
        List<OrderStatistics> list= orderMapper.getOrderStatistics(orderStatistics);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        List<String> dateList = list.stream().map(OrderStatistics::getOrderDate).map(item->{
            String dateStr = simpleDateFormat.format(item);
            return dateStr;
        }).collect(Collectors.toList());
        List<Double> count = list.stream().map(OrderStatistics::getSumIncome).collect(Collectors.toList());
        result.put("dateList", dateList);
        result.put("countList", count);
        return result;
    }
}
